package com.cino.pet.service.impl;

import com.cino.basic.base.mapper.BaseMapper;
import com.cino.basic.base.service.BaiduAiAuditService;
import com.cino.basic.base.service.impl.BaseServiceImpl;
import com.cino.basic.util.AjaxResult;
import com.cino.pet.domain.Pet;
import com.cino.pet.domain.PetDetail;
import com.cino.pet.domain.PetOnlineAuditLog;
import com.cino.pet.mapper.PetDetailMapper;
import com.cino.pet.mapper.PetMapper;
import com.cino.pet.mapper.PetOnlineAuditLogMapper;
import com.cino.pet.service.PetService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */

@Service
//重构增删改查的目的是同时修改多张表
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delDetailByPetId(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }


    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids){
            Pet pet = petMapper.loadById(id);
            Boolean boo1= baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2= baiduAiAuditService.imageAudit(pet.getResources());
            if(boo1 && boo2){//通过
                //改状态和上架时间， 以及添加审核日志
                petMapper.updateOnsaleState(id,new Date());

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);
            }else{
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    @Transactional
    public void offsale(List<Long> ids) {
        //改状态 state = 0; 下架时间 new Date()
        //批量更新，多参数操作 +Param ,mapper里面加
        petMapper.updateOffsaleState(ids,new Date());

    }
}
